package problem1745;

//1745.分割回文串4
//https://leetcode.cn/problems/palindrome-partitioning-iv/

class Solution {
    public boolean checkPartitioning(String s) {
        int n = s.length();
        boolean[][] dp = new boolean[n][n];
        //填写dp表
        for(int i = n-1; i>=0; i--) {
            for(int j = i; j<n; j++) {
                if(i == j) dp[i][j] = true;
                else if(i+1==j && s.charAt(i) == s.charAt(j)) dp[i][j] = true;
                else if(s.charAt(i) == s.charAt(j)) dp[i][j] = dp[i+1][j-1];
            }
        }

        //遍历所以可能的情况
        for(int i = 1; i<n; i++) {
            for(int j = i+1; j<n; j++) {
                if(dp[0][i-1] && dp[i][j-1] &&  dp[j][n-1]) {
                    return true;
                }
            }
        }
        return false;
    }
}